package com.efeichong.common.domain;


import com.efeichong.audit.InsertDefault;
import com.efeichong.util.TransformUtils;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;


/**
 * @author
 * @date 2020/12/03
 * @description 基础类
 */
@Setter
@Getter
@MappedSuperclass
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"})
public class BaseEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @CreationTimestamp
    @Column(updatable = false)
    private Date createTime;

    @UpdateTimestamp
    private Date updateTime;

    @InsertDefault("0")
    @Version
    @Column(name = "version", columnDefinition = "bigint(20) default'0'")
    private Long version;

    /**
     * po转vo
     *
     * @param voClazz          vo的class
     * @param ignoreProperties 忽略拷贝的字段
     * @return
     */
    @SneakyThrows
    public <V> V toVo(Class<V> voClazz, String... ignoreProperties) {
        return TransformUtils.toVo(this, voClazz, ignoreProperties);
    }

    /**
     * vo转po
     *
     * @param poClazz          po的class
     * @param ignoreProperties 忽略拷贝的字段
     * @return
     */
    @SneakyThrows
    public <T> T toPo(Class<T> poClazz, String... ignoreProperties) {
        return TransformUtils.toPo(this, poClazz, ignoreProperties);
    }
}
